package xddq

import (
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

// 检查神兽类型
func (xd *XdDriver) CheckTalentInfoAttribute(idx int, info D.TalentInfo) bool {
	if info.Val == 0 {
		return true
	}
	if xd.UserData.ConfigTalentAttr[2*idx] > 0 {
		if info.Val == xd.UserData.ConfigTalentAttr[2*idx] {
			return true
		}
	}

	if xd.UserData.ConfigTalentAttr[2*idx+1] > 0 {
		if info.Val == xd.UserData.ConfigTalentAttr[2*idx+1] {
			return true
		}
	}
	return false
}

// 检查灵脉品级
func (xd *XdDriver) CheckTalentInfoGrade(idx int, info D.TalentInfo) bool {
	partIdx := proto.GetTalentPartIdx(info.Part)
	currentInfo := xd.UserData.CurrentEquipmentInfo[idx].Talent[partIdx]
	//if currentInfo.Grade == 0 {
	//	log.Infof("灵脉: 未获取到[%s]的灵脉信息!", D.CloneName[idx])
	//	return false
	//}
	grade := int(info.Grade) - int(currentInfo.Grade)
	level := info.Level - currentInfo.Level
	cCombatValue := xd.UserData.CurrentEquipmentInfo[idx].CombatValue
	//判断灵脉不是预设的类型
	if !xd.CheckTalentInfoAttribute(idx, currentInfo) {
		return true
	}
	//星级大于当前装备直接装备
	if grade >= 0 {
		log.Infof("灵脉: 判断[%s]更高的灵脉星级! \n穿戴:%s\n当前:%s", D.CloneName[idx], proto.GetTalentInfoDesc(currentInfo), proto.GetTalentInfoDesc(info))
		return true
	}
	//品级比现在低但是装备等级更高
	if grade == -1 && level > 8 {
		log.Infof("灵脉: 判断[%s]更高的灵脉等级! \n穿戴:%s\n当前:%s", D.CloneName[idx], proto.GetTalentInfoDesc(currentInfo), proto.GetTalentInfoDesc(info))
		return true
	}
	//妖力更高
	if cCombatValue > 100 && xd.UserData.CurrentEquipmentIdx == idx && info.CombatValue > cCombatValue {
		log.Infof("灵脉: 判断[%s]更高的灵脉妖力(+%s)! \n穿戴:%s\n当前:%s", D.CloneName[idx], D.GetCombatValueDesc(info.CombatValue-cCombatValue),
			proto.GetTalentInfoDesc(currentInfo), proto.GetTalentInfoDesc(info))
		return true
	}
	return false
}

// 检查装备是否需要更新
func (xd *XdDriver) CheckTalentNeedUpdate(info D.TalentInfo) (int, bool) {
	needUpdateFlag := false
	idx := 0
	//只要完美
	if info.Grade <= 8 {
		partIdx := proto.GetTalentPartIdx(info.Part)
		if partIdx == 3 { //离
			currentInfo := xd.UserData.CurrentEquipmentInfo[idx].Talent[partIdx]
			if currentInfo.Level > info.Level && currentInfo.Grade > info.Grade {
				return 0, true
			}
		}
		return idx, false
	}
	for ; idx < 3; idx++ {
		//判断装备属性
		flag := xd.CheckTalentInfoAttribute(idx, info)
		if !flag {
			continue
		}
		//判断装备等级战力
		needUpdateFlag = xd.CheckTalentInfoGrade(idx, info)
		if !xd.UserData.EquipmentCloneFlag || needUpdateFlag {
			break
		}
	}
	return idx, needUpdateFlag
}

func (xd *XdDriver) CheckTalentInfoData(info D.TalentInfo) {

}

func (xd *XdDriver) CheckCurrentTalentInfoData(allInfo D.CurrentTalentInfo) {
	xd.UserData.CurrentEquipmentInfo[xd.UserData.CurrentEquipmentIdx].Talent = allInfo.Talent
}
